package Query;

use strict;

sub preprocess {
  # query is a string with one or more words
  my $query = shift;
  # each search term is a single word or phrase
  my @searchTerms =(); 
  # extract all negated phrases with a while loop
  # +? means ungreedy one or more (+ is greedy)
  while ($query =~ s/!\"(.+?)\"//){
    push(@searchTerms, "!".$1);
  }

  # extract all phrases
  while ($query =~ s/\"(.+?)\"//){
    push(@searchTerms, $1);
  }

  # add all words and negated words
  my @words = grep length, split /\s+/, $query;
  push(@searchTerms, @words);
  
  return @searchTerms;
}

1;
